package fi.jamk.ourcookingapp.activity;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import fi.jamk.ourcookingapp.R;
import fi.jamk.ourcookingapp.backend.MyTools;
import fi.jamk.ourcookingapp.backend.architecture.OurCookingFacade;
import fi.jamk.ourcookingapp.backend.model.User;

/**
 * Activity for holding the tabs and saving user
 * @author Marius
 */
@SuppressWarnings("deprecation")
public class AppActivity extends TabActivity {

	public static OurCookingFacade facade = new OurCookingFacade();
	
	public static TabHost tabHost;
	
	public static final String SAVED_USER = "saveduser";
	
	public static final String SAVED_USER_NAME = "name";
	public static final String SAVED_USER_EMAIL = "email";
	public static final String SAVED_USER_ID = "userID";

	public static final String SAVED_USER_PASSWORD = "userpassword";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		tabHost = getTabHost();
		TabSpec streamSpec = tabHost.newTabSpec("tab1");
		streamSpec.setIndicator(getResources().getString(R.string.tabStream));
		Intent iStream = new Intent(AppActivity.this, StreamActivity.class);
		streamSpec.setContent(iStream);
		
		TabSpec favouritesSpec = tabHost.newTabSpec("tab2");
		favouritesSpec.setIndicator(getResources().getString(R.string.tabFavourites));
		Intent iFavourites = new Intent(AppActivity.this, FavouritesActivity.class);
		favouritesSpec.setContent(iFavourites);
		
		TabSpec filterSpec = tabHost.newTabSpec("tab3");
		filterSpec.setIndicator(getResources().getString(R.string.tabFilter));
		Intent iFilter = new Intent(AppActivity.this, FilterActivity.class);
		filterSpec.setContent(iFilter);
		
		tabHost.addTab(streamSpec);
		tabHost.addTab(favouritesSpec);
		tabHost.addTab(filterSpec);
		
		if(MyTools.isEmpty(getSharedPreferences(SAVED_USER, Context.MODE_PRIVATE).getString(SAVED_USER_NAME, ""))){
			User user = facade.getCurrentUser();
			saveUser(user.getLoginName(), user.getEMail(), user.getPassword(), user.getID());
		}
	}
	
	/**
	 * Store user
	 * @param loginName
	 * @param email
	 * @param password
	 * @param id
	 */
	private void saveUser(String loginName, String email, String password, int id) {
		SharedPreferences savingUser = getSharedPreferences(SAVED_USER, Context.MODE_PRIVATE);
		Editor editor = savingUser.edit();
		editor.putString(SAVED_USER_NAME, loginName);
		editor.putString(SAVED_USER_EMAIL, email);
		editor.putString(SAVED_USER_PASSWORD, password);
		editor.putInt(SAVED_USER_ID, id);
		editor.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.layout.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent;
		switch (item.getItemId()) {
			case R.id.menu_sorting:
				intent = new Intent(this, SortingActivity.class);
				startActivity(intent);
				return true;
			case R.id.menu_addReceipt:
				intent = new Intent(this, AddReceiptActivity.class);
				startActivity(intent);
				return true;
			case R.id.menu_logout:
				saveUser(null, null, null, 0);
				intent = new Intent(this, MainActivity.class);
				startActivity(intent);
				finish();
				return true;
			case R.id.menu_friendList:
				intent = new Intent(this, FriendListActivity.class);
				startActivity(intent);
				return true;
			default:
				break;
			}
		return super.onOptionsItemSelected(item);
	}
	
}
